package com.example.user3.demo

import android.os.Bundle
import android.os.Message
import android.os.PersistableBundle
import android.util.Log
import androidx.fragment.app.FragmentActivity
import com.example.user3.demo.eventbus.Subscribe
import com.example.user3.demo.eventbus.ThreadMode
import com.example.user3.demo.eventbus.ZEventBus
import com.example.user3.demo.reflect.ReflectClass
import kotlinx.android.synthetic.main.activity_new.*
import java.lang.Exception


class TestActivityNew : FragmentActivity() {
    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
        setContentView(R.layout.activity_new)

        ZEventBus.getInstance().register(this);
        try {
            // 创建对象
            ReflectClass.reflectNewInstance()

            // 反射私有的构造方法
            ReflectClass.reflectPrivateConstructor()

            // 反射私有属性
            ReflectClass.reflectPrivateField()

            // 反射私有方法
            ReflectClass.reflectPrivateMethod()
        } catch (ex: Exception) {
            ex.printStackTrace()
        }


        button2.setOnClickListener {
            Log.d("TAG", " zenmode = " + ReflectClass.getZenMode())
            Thread{
                ZEventBus.getInstance().send(Message(1, "activity启动了"))
            }.start()
        }
        button3.setOnClickListener {
            Log.d("TAG", " zenmode = " + ReflectClass.getZenMode())
            Thread{
                ZEventBus.getInstance().send(Message(2, "activity启动了_background"))
            }.start()
        }

        hahha.setOnClickListener {
            ZEventBus.getInstance().send(Message(2, "activity启动了"))
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun eventBus( message:com.example.user3.demo.Message) {
        Log.i("event_bus", message.title);
    }

    @Subscribe(threadMode = ThreadMode.BACKGROUND)
    fun eventBusBackground( message:com.example.user3.demo.Message) {
        Log.i("event_bus2", message.title);
    }
    override fun onDestroy() {
        super.onDestroy()
        ZEventBus.getInstance().unRegister(this);
    }


}
class Message(var num:Int,var title:String)